# -*- coding: utf-8 -*-
from google.appengine.api import urlfetch
from lxml import etree

class Prase():
    url = None
    xmlTree = None
    htmlPage = None

    def __init__(self, url):
        self.url = url
        self.xmlTree = None
        self.htmlPage = None

    def getWebContent(self, url):
        try:
            result = urlfetch.fetch(url)
        except:
            return None
        if result.status_code == 200:
            return result.content
        else:
            return None

    def setXmlTree(self):
        content = self.getWebContent(self.url)
        if content is not None:
            self.xmlTree = etree.fromstring(content)

    def getValuesByPath(self, path):
        self.setXmlTree()
        elements = self.xmlTree.xpath(path)
        values = []
        for item in elements:
            values.append(item.text)
        return values

    def setHtmlPage(self):
        content = self.getWebContent(self.url)
        if content is not None:
            self.htmlPage = etree.HTML(content)
